﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class EDetaeil : System.Web.UI.Page
{
    int UserID;
    protected void Page_Load(object sender, EventArgs e)
    {
        string employeeId = Request["ID"];
        int permissions = int.Parse(Session["permissions"].ToString());
        UserID = int.Parse(Session["employeeID"].ToString());

        getEmployeeDetail(employeeId, permissions);
    }

    /// <summary>
    /// 获得员工详细信息
    /// </summary>
    /// <param name="id">员工ID号</param>
    private void getEmployeeDetail(string id,int permissions)
    {
        if (String.IsNullOrEmpty(id))
            return;

        EmployeeInfo employeeInfo = EmployeeInfo.Getworker(Int32.Parse(id));

        Name.Text = employeeInfo.Name;                                //姓名
        Department.Text = getPartmentOrPosition(id, 0);                  //部门
        //Position.Text = employeeInfo.Position;


        //Position.Text = " SELECT  tb_Position.Position FROM tb_Position inner join tb_EmployeeInfo on tb_Position.ID=tb_EmployeeInfo.Position ";  //职务
        Position.Text = getPartmentOrPosition(id, 1);
        if (employeeInfo.Sex == "0")                              //性别
            Sex.Text = "男";
        else
            Sex.Text = "女";                                    //性别                 


        BirthdayYang.Text = employeeInfo.BirthdayYang;                //出生日期


        if (employeeInfo.BloodType == "1")                            //血型
            BloodType.Text = "A";
        else
            if (employeeInfo.BloodType == "2")
                BloodType.Text = "B";
            else
                if (employeeInfo.BloodType == "3")
                    BloodType.Text = "AB";
                else
                    BloodType.Text = "O";                        //血型

        Nation.Text = employeeInfo.Nation;                            //民族
        Origin.Text = employeeInfo.Origin;                            //籍贯
        Political.Text = employeeInfo.Political;                      //政治面貌
        GraduationTime.Text = employeeInfo.GraduationTime;            //毕业时间
        GraduationSchool.Text = employeeInfo.GraduationSchool;        //毕业院校
        Edu1.Text = employeeInfo.Edu1;                                //第一学历
        Edu1Professional.Text = employeeInfo.Edu1Professional;        //第一学历专业
        Edu1Degree.Text = getDegree1(id);                     //第一学历学位
        Edu2.Text = employeeInfo.Edu2;                                //第二学历
        Edu2Professional.Text = employeeInfo.Edu2Professional;                        //第二学历专业
        Edu2Degree.Text = getDegree2(id);                        //第二学历学位
        ForLanLev.Text = employeeInfo.ForLanLev;                        //外语水平
        ComLev.Text = employeeInfo.ComLev;                        //计算机水平
        IDCard.Text = employeeInfo.IDCard;                        //身份证号
        PerReside.Text = employeeInfo.PerReside;                        //户口所在地
        Marrige.Text = employeeInfo.Marrige;                        //婚姻状况
        WorkExper.Text = employeeInfo.WorkExper;                        //工作年限
        Health.Text = employeeInfo.Health;                        //健康状况
        Address.Text = employeeInfo.Address;                        //详细住址
        OtherAddress.Text = employeeInfo.OtherAddress;                        //其他住址
        Phone.Text = employeeInfo.Phone;                        //座机
        TelePhone.Text = employeeInfo.TelePhone;                        //手机
        EMail.Text = employeeInfo.EMail;                        //E-mail
        QQ.Text = employeeInfo.QQ;                        //QQ
        EmerPer.Text = employeeInfo.EmerPer;                        //紧急时刻联系人
        EmerPhone.Text = employeeInfo.EmerPhone;                        //紧急时刻联系电话
        WorkTime.Text = employeeInfo.WorkTime;                        //参加工作时间
        PostTime.Text = employeeInfo.PostTime;                        //正式上岗时间
        Rec.Text = employeeInfo.Rec;                        //是否有公司员工推荐
        RecName.Text = employeeInfo.RecName;                        //推荐人
        Fam.Text = employeeInfo.Fam;                        //公司内部是否有家属
        FamName.Text = employeeInfo.FamName;                        //家属姓名
        danwei.Text = employeeInfo.danwei;                        //单位                                    没有
        FamPosition.Text = employeeInfo.FamPosition;                        //职务
        Experience.Text = employeeInfo.Experience;                        //工作经历
        Honors.Text = employeeInfo.Honors;                        //所获荣誉
        SocialRelation.Text = employeeInfo.SocialRelation;                        //社会关系
        InAndHo.Text = employeeInfo.InAndHo;                        //兴趣特长
        SelfAsse.Text = employeeInfo.SelfAsse;                        //自我评价

        /*switch (permissions)
        {
            case 1:
            case 2:
            case 3:
                Wage.Text = employeeInfo.Wage;
                break;
            case 4:
            case 5:
            case 6:
                Wage.Text = "****";
                break;

        }*/

        if (permissions == 1 || permissions == 2 || permissions == 3)
        {
            Wage.Text = employeeInfo.Wage;
        }
        else
        {
            if (int.Parse(id) == UserID)
            {
                Wage.Text = "****";
            }
            else
            {
                Wage.Text = employeeInfo.Wage;
            }
        }

        //Wage.Text = employeeInfo.Wage;

        if (!String.IsNullOrEmpty(employeeInfo.Pic))
        {
            String path = ("Pic/" + employeeInfo.Pic);
            Image1.ImageUrl = path;
        }
        else
            Image1.ImageUrl = "";
    }


    /// <summary>
    /// 创建数据库连接
    /// </summary>
    /// <returns></returns>
    private SqlConnection CreateConn()
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
        SqlConnection conn = new SqlConnection(connStr);

        return conn;
    }


    /// <summary>
    /// 获得员工所在部门或职位
    /// </summary>
    /// <param name="id">员工id</param>
    /// <param name="flag">标记 0代表部门，1代表职位</param>
    /// <returns></returns>
    private string getPartmentOrPosition(string id, int flag)
    {
        SqlConnection conn = CreateConn();
        string selentCmdStr;

        if (flag == 0)
        {
            selentCmdStr = "SELECT Department FROM tb_Department WHERE ID=(SELECT tb_EmployeeInfo.Department FROM tb_EmployeeInfo WHERE ID='" + id + "')";
        }
        else
        {
            selentCmdStr = "SELECT Position FROM tb_Position WHERE ID=(SELECT tb_EmployeeInfo.Position FROM tb_EmployeeInfo WHERE ID='" + id + "')";
        }

        SqlCommand selectCmd = new SqlCommand(selentCmdStr, conn);

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = selectCmd;

        DataSet ds = new DataSet();
        da.Fill(ds);

        string result = ds.Tables[0].Rows[0][0].ToString();

        return result;
    }

    private string getDegree1(string id)
    {
        SqlConnection conn = CreateConn();
        string selentCmdStr;


        selentCmdStr = "SELECT Degree FROM tb_Degree WHERE ID=(SELECT tb_EmployeeInfo.Edu1Degree FROM tb_EmployeeInfo WHERE ID='" + id + "')";


        SqlCommand selectCmd = new SqlCommand(selentCmdStr, conn);

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = selectCmd;

        DataSet ds = new DataSet();
        da.Fill(ds);

        string result = ds.Tables[0].Rows[0][0].ToString();

        return result;
    }


    private string getDegree2(string id)
    {
        SqlConnection conn = CreateConn();
        string selentCmdStr;


        selentCmdStr = "SELECT Degree FROM tb_Degree WHERE ID=(SELECT tb_EmployeeInfo.Edu2Degree FROM tb_EmployeeInfo WHERE ID='" + id + "')";


        SqlCommand selectCmd = new SqlCommand(selentCmdStr, conn);

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = selectCmd;

        DataSet ds = new DataSet();
        da.Fill(ds);

        string result = ds.Tables[0].Rows[0][0].ToString();

        return result;
    }


}